package com.fs.book.web;

import com.fs.book.entity.User;
import com.fs.book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("users")
//@Controller
//@ResponseBody
@RestController  //@Controller + @ResponseBody
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("user/{id}")
    //@ResponseBody
    public User queryById(@PathVariable("id") Integer id){

        return null;
    }


    @GetMapping("user")
    //@ResponseBody
    public List<User> queryAll(){
        return null;
    }

    @PostMapping("user")
    //@ResponseBody
    public Map addUser(User user){
        return null;
    }

    @PostMapping("login")
    //@ResponseBody
    public Map login(String username, String password, HttpSession session) throws Exception{
        //status: 状态码: 200 成功   token: 用户名   510 登录失败   msg: 错误信息
        Map map = new HashMap();
        User user = userService.login(username, password);
        map.put("status",200);
        session.setAttribute("token",user.getLoginname());
        map.put("token",user.getLoginname());
        return map;
    }


    @PostMapping("/register")
    @ResponseBody
    public Map register(User user){
        Map map = new HashMap();
        try{
            userService.register(user);
            map.put("status",200);
            map.put("message","注册成功");
        }catch (Exception e){
            map.put("status",500);
            map.put("message","服务器出错了");
        }
        return map;
    }



    @DeleteMapping("user/{id}")
    //@ResponseBody
    public Map deleteById(@PathVariable("id") Integer id, @RequestHeader("token") String token, @CookieValue("k1") String k1){
        return null;
    }

    @PutMapping("user")
    //@ResponseBody
    public Map updateUser(User user){
        return null;
    }
}
